package gaileenchess;

public class Common
{
    public enum Side {WHITE, BLACK} // the colour of a piece. Can also apply to a player.
    
    
    // converts a string of the form (A-H)(1-8) (as given for some commands) into a Board.Position, or returns
    //   null if the string cannot be converted
    public static Board.Position stringToPos(String str)
    {
        if(str.length() != 2)
        {
            return null;
        }
        
        str = str.toUpperCase();
        
        // we start at the second character for the row, 48 is the ascii value of '0', but we want this to be 0-indexed for the
        //   row, and we get it in as (1-8), so we have to use 49 here
        int r = (int)str.charAt(1) - 49;
        // the first character is the column, should be an upper case letter, so we subtract 65, the ascii value of 'A'
        int c = (int)str.charAt(0) - 65;
        
        // make sure this is valid (ie, we didn't get in a 0, 9, (I-Z), or non-alphanumeric symbol)
        if(!(r >= 0 && r < 8 && c >= 0 && c < 8))
        {
            return null;
        }
        
        return new Board.Position(r, c);
    }
}
